﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CycleCountClassBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountClassDA cycleCountClassDA = DataAccess.CreateCycleCountClassDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CycleCountClassBLL()
        {
            this.pubParasInfo = null;
        }

        public CycleCountClassBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CycleCountClassBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> classIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CycleCountClassInfo> cycleCountClassInfos = new List<CycleCountClassInfo>();
                CycleCountClassInfo cycleCountClassInfo = null;
                foreach (string id in classIDs)
                {
                    cycleCountClassInfo = cycleCountClassDA.GetCycleCountClassByID(dataBase, tran, id, this.dataBaseID[0]);
                    cycleCountClassInfos.Add(cycleCountClassInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (cycleCountClassDA.DeleteCycleCountClass(dataBase, tran, classIDs, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < classIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CycleCountClassInfo>(dataBase, tran, cycleCountClassInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CYCLE_COUNT_CLASS", this.dataBaseID);
                        temp = temp + classIDs[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return cycleCountClassDA.GetAllCycleCountClass(this.dataBaseID[0]);
        }

        public object GetInfoByID(string classID)
        {
            return cycleCountClassDA.GetCycleCountClassByID(classID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            CycleCountClassQueryEntity cycleCountClassQuery = obj as CycleCountClassQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(cycleCountClassQuery.ClassID))
            {
                sqlWhere.Add(" CLASS_ID " + cycleCountClassQuery.ClassID);
            }
            if (!string.IsNullOrEmpty(cycleCountClassQuery.CycleDays))
            {
                sqlWhere.Add(" CYCLE_DAYS " + cycleCountClassQuery.CycleDays);
            }
            if (!string.IsNullOrEmpty(cycleCountClassQuery.PosQtyVariance))
            {
                sqlWhere.Add(" POS_QTY_VARIANCE " + cycleCountClassQuery.PosQtyVariance);
            }
            if (!string.IsNullOrEmpty(cycleCountClassQuery.NegCntVariance))
            {
                sqlWhere.Add(" NEG_CNT_VARIANCE " + cycleCountClassQuery.NegCntVariance);
            }
            if (!string.IsNullOrEmpty(cycleCountClassQuery.PosValueVariance))
            {
                sqlWhere.Add(" POS_VALUE_VARIANCE " + cycleCountClassQuery.PosValueVariance);
            }
            if (!string.IsNullOrEmpty(cycleCountClassQuery.NegValueVariance))
            {
                sqlWhere.Add(" NEG_VALUE_VARIANCE " + cycleCountClassQuery.NegValueVariance);
            }
            if (!string.IsNullOrEmpty(cycleCountClassQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + cycleCountClassQuery.IsActive);
            }
            return cycleCountClassDA.GetCycleCountClassByQueryList(sqlWhere, cycleCountClassQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string classID = string.Empty;
            try
            {
                CycleCountClassInfo cycleCountClass = obj as CycleCountClassInfo;
                if (cycleCountClassDA.CheckCycleCountClassIDUnique(dataBase, tran, cycleCountClass.ClassID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2620_001", this.pubParasInfo.UserLanguage, new List<string> { cycleCountClass.ClassID });
                }
                cycleCountClass.CreatedBy = this.pubParasInfo.UserID;
                cycleCountClass.UpdatedBy = this.pubParasInfo.UserID;
                cycleCountClass.CreatedDate = DateTime.Now;
                cycleCountClass.UpdatedDate = DateTime.Now;
                cycleCountClassDA.InsertCycleCountClass(dataBase, tran, cycleCountClass, this.dataBaseID);
                new AuditDataBLL().InsertAuditData<CycleCountClassInfo>(dataBase, tran, null, cycleCountClass, AuditAction.Insert, this.pubParasInfo.UserID, "CYCLE_COUNT_CLASS", this.dataBaseID);
                classID = cycleCountClass.ClassID;
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return classID;
        }

        public string Update(object obj)
        {
            string classID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CycleCountClassInfo cycleCountClass = obj as CycleCountClassInfo;
                CycleCountClassInfo oldCycleCountClassInfo = cycleCountClassDA.GetCycleCountClassByID(dataBase, tran, cycleCountClass.ClassID, this.dataBaseID[0]);
                if (cycleCountClassDA.UpdateCycleCountClass(dataBase, tran, cycleCountClass, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CycleCountClassInfo>(dataBase, tran, oldCycleCountClassInfo, cycleCountClass, AuditAction.Update, this.pubParasInfo.UserID, "CYCLE_COUNT_CLASS", this.dataBaseID);
                    classID = cycleCountClass.ClassID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return classID;
        }
    }
}

